package com.ltkj.baseplatform.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ltkj.baseplatform.domain.bo.HomeRecommendTypeBo;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.baseplatform.mapper.HomeRecommendTypeMapper;
import com.ltkj.baseplatform.domain.HomeRecommendType;
import com.ltkj.baseplatform.service.IHomeRecommendTypeService;

/**
 * 综合推荐分类Service业务层处理
 * 
 * @author liupf
 * @date 2023-09-01
 */
@Service
public class HomeRecommendTypeServiceImpl implements IHomeRecommendTypeService 
{
    @Autowired
    private HomeRecommendTypeMapper homeRecommendTypeMapper;

    /**
     * 查询综合推荐分类
     * 
     * @param id 综合推荐分类主键
     * @return 综合推荐分类
     */
    @Override
    public HomeRecommendType selectHomeRecommendTypeById(Long id)
    {
        return homeRecommendTypeMapper.selectHomeRecommendTypeById(id);
    }

    /**
     * 查询综合推荐分类列表
     * 
     * @param homeRecommendType 综合推荐分类
     * @return 综合推荐分类
     */
    @Override
    public List<HomeRecommendType> selectHomeRecommendTypeList(HomeRecommendType homeRecommendType)
    {
        return homeRecommendTypeMapper.selectHomeRecommendTypeList(homeRecommendType);
    }

    /**
     * 新增综合推荐分类
     * 
     * @param homeRecommendType 综合推荐分类
     * @return 结果
     */
    @Override
    public int insertHomeRecommendType(HomeRecommendType homeRecommendType)
    {
        homeRecommendType.setCreateTime(DateUtils.getNowDate());
        return homeRecommendTypeMapper.insertHomeRecommendType(homeRecommendType);
    }

    /**
     * 修改综合推荐分类
     * 
     * @param homeRecommendType 综合推荐分类
     * @return 结果
     */
    @Override
    public int updateHomeRecommendType(HomeRecommendType homeRecommendType)
    {
        homeRecommendType.setUpdateTime(DateUtils.getNowDate());
        return homeRecommendTypeMapper.updateHomeRecommendType(homeRecommendType);
    }
    /**
     * 使用Bo新增综合推荐分类
     *
     * @param homeRecommendTypeBo 综合推荐分类
     * @return 结果
     */
    @Override
    public int insertHomeRecommendType(HomeRecommendTypeBo homeRecommendTypeBo) {
        HomeRecommendType homeRecommendType = new HomeRecommendType();
        BeanUtils.copyProperties(homeRecommendTypeBo,homeRecommendType);
        homeRecommendType.setCreateTime(DateUtils.getNowDate());
        homeRecommendType.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return homeRecommendTypeMapper.insertHomeRecommendType(homeRecommendType);
    }
    /**
     * 使用Bo修改综合推荐分类
     *
     * @param homeRecommendTypeBo 综合推荐分类
     * @return 结果
     */
    @Override
    public int updateHomeRecommendType(HomeRecommendTypeBo homeRecommendTypeBo) {
        HomeRecommendType homeRecommendType = new HomeRecommendType();
        BeanUtils.copyProperties(homeRecommendTypeBo,homeRecommendType);
        homeRecommendType.setUpdateTime(DateUtils.getNowDate());
        homeRecommendType.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return homeRecommendTypeMapper.updateHomeRecommendType(homeRecommendType);
    }

    /**
     * 批量删除综合推荐分类
     * 
     * @param ids 需要删除的综合推荐分类主键
     * @return 结果
     */
    @Override
    public int deleteHomeRecommendTypeByIds(Long[] ids)
    {
        return homeRecommendTypeMapper.deleteHomeRecommendTypeByIds(ids);
    }

    /**
     * 删除综合推荐分类信息
     * 
     * @param id 综合推荐分类主键
     * @return 结果
     */
    @Override
    public int deleteHomeRecommendTypeById(Long id)
    {
        return homeRecommendTypeMapper.deleteHomeRecommendTypeById(id);
    }
    /**
     * 查询推荐分类树结构信息
     *
     * @return 推荐分类树信息集合
     */
    @Override
    public List<HomeRecommendType> selectRecommendTreeList() {
        List<HomeRecommendType> list = homeRecommendTypeMapper.selectHomeRecommendTypeList(new HomeRecommendType());
        List<HomeRecommendType> trees = buildRecommendTree(list);
        return trees;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param artChannels 栏目列表
     * @return 树结构列表
     */
    private List<HomeRecommendType> buildRecommendTree(List<HomeRecommendType> artChannels)
    {
        List<HomeRecommendType> returnList = new ArrayList<>();
        List<Long> tempList = artChannels.stream().map(HomeRecommendType::getId).collect(Collectors.toList());
        for (HomeRecommendType channel : artChannels)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(channel.getParentId()))
            {
                recursionFn(artChannels, channel);
                returnList.add(channel);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = artChannels;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<HomeRecommendType> list, HomeRecommendType t)
    {
        // 得到子节点列表
        List<HomeRecommendType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (HomeRecommendType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<HomeRecommendType> getChildList(List<HomeRecommendType> list, HomeRecommendType t)
    {
        List<HomeRecommendType> tlist = new ArrayList<>();
        Iterator<HomeRecommendType> it = list.iterator();
        while (it.hasNext())
        {
            HomeRecommendType n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<HomeRecommendType> list, HomeRecommendType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
